package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.OrderSetting;
import com.xrit.sunnyday.service.OrderSettingService;
import com.xrit.sunnyday.utils.POIUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 预约设置
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 上传excel文件
     * 1.处理excel对应的实体类数据
     * 2.判断Java实体类数据是否符合插入数据库的要求
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            // 1.工具类读取excel内容
            List<String[]> excelStringList = POIUtils.readExcel(excelFile);
            // 5.转成List<OrderSetting>供服务层使用
            List<OrderSetting> orderSettingList = new ArrayList<OrderSetting>();
            //3.日期格式解析 POIUtils把DATE_FORMAT属性该为public
            SimpleDateFormat sdf = new SimpleDateFormat(POIUtils.DATE_FORMAT);
            Date orderDate = null;
            OrderSetting os = null;
            //2.将List<String[]>，转换成List<OrderSetting>
            //String[]:第一个是日期 对应转化成OrderSetting中的Date
            //第二个是数值字符串 转化成OrderSetting中的int
            for (String[] dataArr : excelStringList) {
                //3.1 格式化模板中每行第1个单元格数据:时间字符串类型 Date
                orderDate = sdf.parse(dataArr[0]);
                //3.2 格式化模板中每行第2个单元格数据：数字字符串类型 int
                int number = Integer.valueOf(dataArr[1]);
                //4.将已经符合我们要求的值添加到OrderSetting对象
                os = new OrderSetting(orderDate,number);
                //6.将OrderSetting对象添加进集合
                orderSettingList.add(os);
            }
            //7.将List<OrderSetting>作为参数传给业务服务处理添加到数据库
            orderSettingService.add(orderSettingList);
            //8.成功
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //9.失败
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据日期查询预约设置数据  获取指定日期的所在月份的预约设置数据
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month){
        // 调用服务端查询
        List<Map<String,Integer>> data = orderSettingService.getOrderSettingByMonth(month);
        return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,data);
    }

    /**
     * 基于日历的预约设置(修改可预约人数)
     */
    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        //调用服务
        orderSettingService.editNumberByDate(orderSetting);
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }

}
